<?php
include "include/config.inc.php";

$filterOnSelected = (isset($_REQUEST['filterOn']) ? $_REQUEST['filterOn'] : "receiveDate");

$today = date("m/d/y");
$installmentArr = array();
$loanIdVar = "";
$selectedParty = 0;
$toatalAmount = 0;
$m=0;
$selectParty = "SELECT a.loanId,b.partyId,c.partyName
                  FROM installment a
	                JOIN loan b ON a.loanId = b.loanId
	                JOIN party c ON b.partyId = c.partyId
	               WHERE a.isReceived = 'Y'
	            GROUP BY b.partyId
                 ORDER BY loanId";
$selectPartyRes = mysql_query($selectParty);
while($selectPartyRow = mysql_fetch_array($selectPartyRes))
{
  $selectPartyId[$m]   = $selectPartyRow['partyId'];
  $selectPartyName[$m] = $selectPartyRow['partyName'];
  $m++;
}

/////////////////  Date filter variable define start
if(isset($_REQUEST['fromDateYear']))
	$fromDate  = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'];
else
	$fromDate  = date("Y-m-d");

if(isset($_REQUEST['toDateYear']))
	$toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'];
else
	$toDate    = date("Y-m-d");
/////////////////  Date filter variable define end

if(isset($_REQUEST['go']))
{
	$selectedParty = $_REQUEST['partyNameSel'];
	$loanIdVar=($_REQUEST['loanId'] == "")?0:$_REQUEST['loanId'];
	if($loanIdVar == 0)
	{
    $i=0;
	  if($_REQUEST['partyNameSel'] == 0)
	  {
 	    $selInstallmentDate = "SELECT a.loanId, a.installmentId,a.loanId,a.installmentNo,a.installmentDate,a.installmentAmount,a.isReceived,b.partyId,c.partyName
	                             FROM installment a
	                             JOIN loan b ON a.loanId = b.loanId
	                             JOIN party c ON b.partyId = c.partyId
	                            WHERE 1=1
	                              AND a.".$filterOnSelected." >= '".$fromDate."'
	                              AND a.".$filterOnSelected." <= '".$toDate."'
	                              AND a.isReceived = 'Y'
                           ORDER BY loanId,installmentId";
	  }
	  else
	  {
	  	$partyId = $_REQUEST['partyNameSel'];
	    $selInstallmentDate = "SELECT a.loanId, a.installmentId,a.loanId,a.installmentNo,a.installmentDate,a.installmentAmount,a.isReceived,b.partyId,c.partyName
	                             FROM installment a
	                             JOIN loan b ON a.loanId = b.loanId
	                             JOIN party c ON b.partyId = c.partyId
	                            WHERE 1=1
	                              AND a.".$filterOnSelected." >= '".$fromDate."'
	                              AND a.".$filterOnSelected." <= '".$toDate."'
	                              AND a.isReceived = 'Y'
	                              AND b.partyId = ".$partyId."
                           ORDER BY loanId,installmentId" ;
	  }
	  $selInstallmentDateRes=mysql_query($selInstallmentDate);
    $currentParyId = 0;
	  while($selInstallmentDateResRow=mysql_fetch_array($selInstallmentDateRes))
	  {
      $installmentArr[$i]['loanId']            = $selInstallmentDateResRow['loanId'];
	    $installmentArr[$i]['installmentNo']     = $selInstallmentDateResRow['installmentNo'];
	    $installmentArr[$i]['installmentId']     = $selInstallmentDateResRow['installmentId'];
	    $installmentArr[$i]['partyName']         = $selInstallmentDateResRow['partyName'];
	    $installmentArr[$i]['loanId']            = $selInstallmentDateResRow['loanId'];
	    $installmentArr[$i]['installmentDate']   = date("d-m-Y", strtotime($selInstallmentDateResRow['installmentDate']));
	    $installmentArr[$i]['installmentAmount'] = $selInstallmentDateResRow['installmentAmount'];
	    $installmentArr[$i]['isReceived']        = $selInstallmentDateResRow['isReceived'];
	    $toatalAmount									          += $selInstallmentDateResRow['installmentAmount'];
	    $i++;
	  }
  }
  else
  {
    $i=0;
    $selInstallmentDate = "SELECT a.installmentId,a.loanId,a.installmentNo,a.installmentDate,a.installmentAmount,a.isReceived,b.partyId,c.partyName
	                           FROM installment a
	                           JOIN loan b ON a.loanId = b.loanId
	                           JOIN party c ON b.partyId = c.partyId
	                          WHERE 1=1
                              AND a.".$filterOnSelected." >= '".$fromDate."'
                              AND a.".$filterOnSelected." <= '".$toDate."'
                              AND a.isReceived = 'Y' AND a.loanId='$loanIdVar'
                         ORDER BY loanId,installmentId";
	  $selInstallmentDateRes=mysql_query($selInstallmentDate);
        $currentParyId = 0;
	  while($selInstallmentDateResRow=mysql_fetch_array($selInstallmentDateRes))
	  {
      $installmentArr[$i]['installmentNo']     = $selInstallmentDateResRow['installmentNo'];
	    $installmentArr[$i]['installmentId']     = $selInstallmentDateResRow['installmentId'];
	    $installmentArr[$i]['partyName']         = $selInstallmentDateResRow['partyName'];
	    $installmentArr[$i]['loanId']            = $selInstallmentDateResRow['loanId'];
	    $installmentArr[$i]['installmentDate']   = date("d-m-Y", strtotime($selInstallmentDateResRow['installmentDate']));
	    $installmentArr[$i]['installmentAmount'] = $selInstallmentDateResRow['installmentAmount'];
	    $installmentArr[$i]['isReceived']        = $selInstallmentDateResRow['isReceived'];
	    $toatalAmount									          += $selInstallmentDateResRow['installmentAmount'];
	    $i++;
	  }
  	
  	
  }
}

if(isset($_POST['sub']))
{
  $arrIA=count($_POST['installmentAmount']);
  $installmentValue=$_POST['InstallmentAmountVal']."<br>";
  $installmentDate = $_POST['installmentDate'];
  for($i=0;$i<$arrIA;$i++)
  {
  	if($_POST['installmentAmount'][$i] != "")
  	{
  		$countLoop=$_POST['installmentAmount'][$i]/$installmentValue=$_POST['InstallmentAmountVal'];
  		for($j=0;$j<$countLoop;$j++)
  		{
        $_POST['installmentAmount'][$i];    	
        $_POST['InstallmentDate'][$i];    	
        $InstallmentsID = $_POST['InstallmentsID'][$i];
        $InstallmentsIDADD = $InstallmentsID + $j;
        $loanId = $_POST['loanId'][$i];
        $updInstallMent="UPDATE `installment` SET  `isReceived` =  'Y', `receiveDate` = '".$installmentDate."'
                          WHERE `installmentId` =".$InstallmentsIDADD." AND loanId=".$loanId;          
        mysql_query($updInstallMent);
      }
  	}
  }
}

//filterOn : Start
$i = 0;
$filterOnValues[$i] = "receiveDate";
$filterOnOutput[$i] = "Receive Date";
$i++;
$filterOnValues[$i] = "installmentDate";
$filterOnOutput[$i] = "Installment Date";
$i++;
//filterOn : End

$smarty->assign("filterOnSelected",$filterOnSelected);
$smarty->assign("filterOnValues",$filterOnValues);
$smarty->assign("filterOnOutput",$filterOnOutput);
$smarty->assign("today",$today);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("toDate",$toDate);
$smarty->assign("selectPartyId",$selectPartyId);
$smarty->assign("selectPartyName",$selectPartyName);
$smarty->assign("selectedParty",$selectedParty);
$smarty->assign("loanIdVar",$loanIdVar);
$smarty->assign("installmentArr",$installmentArr);
$smarty->assign("toatalAmount",$toatalAmount);
$smarty->display('loanreceived.tpl');
?>